package com.kaili.basic.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseCarrierAddDto;
import com.kaili.basic.pojo.dto.BaseCarrierQueryDto;
import com.kaili.basic.pojo.entity.BaseCarrier;
import com.kaili.basic.service.BaseCarrierService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Api(tags = "承运商管理", description = "承运商管理相关API")
public class BaseCarrierController {

    @Autowired
    private BaseCarrierService carrierService;

    @PostMapping("/carriers")
    @ApiOperation(value = "新增", notes = "新增承运商管理数据")
    public RestStatus addBaseMaintenance(@Validated @RequestBody BaseCarrierAddDto carrierAddDto) {
        if (carrierAddDto == null) {
            return CommonStatus.BAD_REQUEST;
        }
        carrierService.addCarrier(carrierAddDto);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/carriers/{carrierId}")
    @ApiOperation(value = "删除", notes = "删除承运商管理数据")
    public RestStatus deleteBaseMaintenance(@PathVariable("carrierId") Long carrierId) {
        if (carrierId == null) {
            return CommonStatus.BAD_REQUEST;
        }
        carrierService.deleteCarrier(carrierId);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/carriers")
    @ApiOperation(value = "更新", notes = "更新承运商管理数据")
    public RestStatus updateBaseMaintenance(@Validated @RequestBody BaseCarrierAddDto carrierAddDto) {
        if (carrierAddDto != null) {
            carrierService.updateCarrier(carrierAddDto);
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @GetMapping("/carriers")
    @ApiOperation(value = "分页查询承运商管理数据", notes = "分页查询事件")
    public Page<BaseCarrier> queryBaseMaintenance(@ModelAttribute BaseCarrierQueryDto carrierQueryDto) {
        BaseCarrier carrier = new BaseCarrier();
        BeanUtils.copyProperties(carrierQueryDto, carrier);
        return carrierService.queryCarrier(new Page<BaseCarrier>(carrier.getPage(), carrier.getPageSize()), carrier);
    }


    @GetMapping("/carriers/like")
    @ApiOperation(value = "模糊查询承运商")
    public List<BaseCarrier> queryCarrierByLike(@ModelAttribute BaseCarrierQueryDto carrierQueryDto) {
        BaseCarrier carrier = new BaseCarrier();
        BeanUtils.copyProperties(carrierQueryDto, carrier);
        return carrierService.queryCarrierByLike(carrier);
    }

    @GetMapping("/carrierCodes")
    @ApiOperation(value = "编号", notes = "查询编号")
    public String getCarrierCode(){
        String code = carrierService.getCarrierCode();
        if (StringUtils.isEmpty(code)){
            return "00001";
        }

        String result = String.valueOf(Integer.valueOf(code)+1);
        if (result.length() == 1){
            result = "0000" + result;
        }else if (result.length() == 2){
            result = "000" + result;
        }else if (result.length() == 3){
            result = "00" + result;
        }else if (result.length() == 4){
            result = "0" + result;
        }
        return result;
    }
}
